package utility;

import java.awt.Color;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;

import multi.SAUNA.Path;
import multi.SAUNA.TemporalPolyhedron;
import org.metacsp.multi.activity.Activity;
import org.metacsp.multi.allenInterval.AllenIntervalConstraint;
import org.metacsp.framework.ConstraintSolver;


public class MapUpdater extends javax.swing.SwingWorker<Void,Void> {

	private Activity[] activities;
	private JButton animateButton;
	private JPanel panel;
	private Vector<AllenIntervalConstraint> onlineConstraints;
	private Vector<AllenIntervalConstraint> toAddOnlineConstraints;
	private ConstraintSolver cs;
	
	public MapUpdater(Activity[] activities, JButton animateButton, JPanel panel, Vector<AllenIntervalConstraint> toAddOnlineConstraints, Vector<AllenIntervalConstraint> onlineConstraints, ConstraintSolver cs) {
		this.activities = activities;
		this.animateButton = animateButton;
		this.panel = panel;
		this.toAddOnlineConstraints = toAddOnlineConstraints;
		this.onlineConstraints = onlineConstraints;
		this.cs = cs;
	}
	
	private void processOnlineConstraints() {
		//process any constraints added by the interface during animation 
		for (AllenIntervalConstraint con : toAddOnlineConstraints) {
			if (cs.addConstraint(con)) {
				((TemporalPolyhedron)con.getFrom()).getSpatialPolyhedron().setColor(Color.blue);
				((TemporalPolyhedron)con.getTo()).getSpatialPolyhedron().setColor(Color.blue);
				System.out.println("Added constraint " + con);
				onlineConstraints.add(con);
			}
			else {
				System.out.println("Failed to add " + con);
			}
		}
		toAddOnlineConstraints.removeAllElements();
	}
	
	@Override
	protected Void doInBackground() throws Exception {
				
		//make all but last blue by default
		for (Activity act : activities) {
			if (!((TemporalPolyhedron)act).getSpatialPolyhedron().isLast())
				((TemporalPolyhedron)act).getSpatialPolyhedron().setColor(Color.blue);
		}
		
		long startTime = Calendar.getInstance().getTimeInMillis();
		boolean dirty = true;
		while (dirty) {
			processOnlineConstraints();
			dirty = false;
			for (Activity act : activities) {
				if (!((TemporalPolyhedron)act).getSpatialPolyhedron().isInvisible()) {
					double timeElapsed = (Calendar.getInstance().getTimeInMillis()-startTime)/1000.0;
					//if poly is executing...
					if (act.getTemporalVariable().getEST()*Path.TEMPORAL_RESOLUTION <= timeElapsed && act.getTemporalVariable().getEET()*Path.TEMPORAL_RESOLUTION >= timeElapsed) {
						//... if it wasn't already red, say that it's starting
						if (!((TemporalPolyhedron)act).getSpatialPolyhedron().getColor().equals(Color.red)) System.out.println(act + " executing @ " + timeElapsed);
						//... make it red
						((TemporalPolyhedron)act).getSpatialPolyhedron().setColor(Color.red);
						//... and make sure we continue loop
						dirty = true;
					}
					//if poly is not executing...
					else {
						//... and it's red (but not last) 
						if (((TemporalPolyhedron)act).getSpatialPolyhedron().getColor().equals(Color.red) && !((TemporalPolyhedron)act).getSpatialPolyhedron().isLast()) {
							//.. make it blue
							((TemporalPolyhedron)act).getSpatialPolyhedron().setColor(Color.blue);
							//System.out.println(act + " REVERTS TO BLUE and ISLAST? " + ((ConvexPolygon)act).getSpatialPolygon().isLast());
						}
					}
				}
			}
			publish(new Void[] {});//this.myPanel.repaint();
			try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
		}
		//animateButton.setEnabled(true);
		return null;
	}
	
	public void process(List<Void> chunks) {
		panel.repaint();
	}
	
	public void done() {
		animateButton.setEnabled(true);
	}

}
